#!/bin/bash

# Tiny QA - 文档目录配置脚本

echo "========================================"
echo "Tiny QA - 文档目录配置向导"
echo "========================================"

echo ""
echo "📁 请配置您的文档目录路径"
echo ""
echo "支持的文档格式:"
echo "- PDF (.pdf)"
echo "- Word (.docx)" 
echo "- Excel (.xlsx)"
echo "- 文本 (.txt)"
echo "- Markdown (.md)"
echo ""

# 检测操作系统
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
    # Windows
    echo "🪟 检测到Windows系统"
    echo ""
    echo "请选择文档目录:"
    echo "1. 使用默认文档目录 (C:\Users\$USERNAME\Documents)"
    echo "2. 手动输入路径"
    echo "3. 浏览选择目录"
    echo ""
    read -p "请选择 (1-3): " choice
    
    case $choice in
        1)
            DOCS_PATH="C:\\Users\\$USERNAME\\Documents"
            ;;
        2)
            read -p "请输入文档目录路径: " DOCS_PATH
            ;;
        3)
            echo "请手动输入文档目录路径:"
            read -p "路径: " DOCS_PATH
            ;;
        *)
            DOCS_PATH="C:\\Users\\$USERNAME\\Documents"
            ;;
    esac
else
    # macOS/Linux
    echo "🐧 检测到Unix/Linux系统"
    echo ""
    echo "请选择文档目录:"
    echo "1. 使用默认文档目录 ($HOME/Documents)"
    echo "2. 手动输入路径"
    echo "3. 浏览选择目录"
    echo ""
    read -p "请选择 (1-3): " choice
    
    case $choice in
        1)
            DOCS_PATH="$HOME/Documents"
            ;;
        2)
            read -p "请输入文档目录路径: " DOCS_PATH
            ;;
        3)
            echo "请手动输入文档目录路径:"
            read -p "路径: " DOCS_PATH
            ;;
        *)
            DOCS_PATH="$HOME/Documents"
            ;;
    esac
fi

# 验证路径是否存在
if [ ! -d "$DOCS_PATH" ]; then
    echo "❌ 错误: 目录不存在: $DOCS_PATH"
    echo "请检查路径是否正确"
    exit 1
fi

echo ""
echo "✅ 文档目录验证通过: $DOCS_PATH"

# 检查目录中的文档文件
echo ""
echo "🔍 扫描文档文件..."
find "$DOCS_PATH" -type f \( -name "*.pdf" -o -name "*.docx" -o -name "*.xlsx" -o -name "*.txt" -o -name "*.md" \) | head -10

echo ""
echo "📋 找到的文档文件 (显示前10个):"
find "$DOCS_PATH" -type f \( -name "*.pdf" -o -name "*.docx" -o -name "*.xlsx" -o -name "*.txt" -o -name "*.md" \) | head -10

# 更新docker-compose.yml
echo ""
echo "🔧 更新docker-compose.yml配置..."

# 备份原文件
cp docker-compose.yml docker-compose.yml.backup

# 更新配置
if [[ "$OSTYPE" == "msys" || "$OSTYPE" == "win32" ]]; then
    # Windows路径格式
    sed -i "s|# - /path/to/your/documents:/app/data/documents|- $DOCS_PATH:/app/data/documents|" docker-compose.yml
else
    # Unix路径格式
    sed -i "s|# - /path/to/your/documents:/app/data/documents|- $DOCS_PATH:/app/data/documents|" docker-compose.yml
fi

echo "✅ 配置已更新"

echo ""
echo "========================================"
echo "配置完成！"
echo "========================================"
echo ""
echo "📁 文档目录: $DOCS_PATH"
echo "📄 支持的格式: PDF, Word, Excel, TXT, Markdown"
echo ""
echo "🚀 下一步:"
echo "1. 运行 ./scripts/docker-deploy.sh 启动服务"
echo "2. 系统会自动扫描并处理文档"
echo "3. 访问 http://localhost:5002 开始使用"
echo ""
echo "💡 提示: 如果文档目录发生变化，请重新运行此脚本" 